package com.lanlian.cayman.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.lanlian.cayman.R;
import com.lanlian.cayman.fragment.QuestionFragment;
import com.lanlian.cayman.utils.VolleyRequestUtils;
import com.lanlian.commonlib.base.BaseActivity;
import com.lanlian.commonlib.bean.BaseResponse;
import com.lanlian.commonlib.bean.QuestionInfo;
import com.lanlian.commonlib.bean.ReqQuestionList;
import com.lanlian.commonlib.bean.RespQuestionList;
import com.lanlian.commonlib.network.INetWorkCallback;
import com.lanlian.utillib.DisplayUtil;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/8/14 14:25
 *     desc   : 常见问题
 *     version: 1.0
 * </pre>
 */
public class QuestionActivity extends BaseActivity {

    private MagicIndicator mMagicIndicator;
    private ViewPager mViewPager;

    private ArrayList<String> mTitleList = new ArrayList<>();
    private ArrayList<Fragment> mFragments = new ArrayList<>();

    private ArrayList<HashMap<String, ArrayList<QuestionInfo>>> mQuestionMaps = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question, true);
        initView();
        setListeners();
        getQuestionList();
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("常见问题");
        mMagicIndicator = findViewById(R.id.magic_indicator);
        mViewPager = findViewById(R.id.view_pager);
    }

    private void getQuestionList() {
        showLoadingDialog();
        VolleyRequestUtils.getInstance().getQuestionList(this.toString(), new ReqQuestionList(), new INetWorkCallback() {
            @Override
            public void onSuccess(Object response) {
                dismissLoadingDialog();
                RespQuestionList respQuestionList = (RespQuestionList) response;
                mQuestionMaps = respQuestionList.getData();
                initFragments();
                initNavigator();
            }

            @Override
            public void onFail(BaseResponse baseResponse) {
                dismissLoadingDialog();
                showToast(baseResponse.getMsg());
            }
        });
    }

    private void initFragments() {
        for (HashMap<String, ArrayList<QuestionInfo>> mQuestionMap : mQuestionMaps) {
            for (LinkedHashMap.Entry<String, ArrayList<QuestionInfo>> stringArrayListEntry : mQuestionMap.entrySet()) {
                mTitleList.add(stringArrayListEntry.getKey());
                mFragments.add(QuestionFragment.newInstance(stringArrayListEntry.getValue()));
            }
        }
    }

    private void initNavigator() {
        CommonNavigator commonNavigator = new CommonNavigator(this);
        if (mTitleList.size() < 5) {
            commonNavigator.setAdjustMode(true);
        }
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {

            @Override
            public int getCount() {
                return mTitleList == null ? 0 : mTitleList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                ColorTransitionPagerTitleView titleView = new ColorTransitionPagerTitleView(context);
                titleView.setNormalColor(getResources().getColor(R.color.color_text_style22));
                titleView.setTextSize(13);
                titleView.setSelectedColor(getResources().getColor(R.color.color_text_style5));
                titleView.setText(mTitleList.get(index));
                titleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mViewPager.setCurrentItem(index);
                    }
                });
                return titleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setLineHeight(DisplayUtil.dip2px(context, 2));
                indicator.setLineWidth(DisplayUtil.dip2px(context, 24));
                indicator.setRoundRadius(DisplayUtil.dip2px(context, 1));
                indicator.setYOffset(DisplayUtil.dip2px(context, 1));
                indicator.setColors(getResources().getColor(R.color.color_text_style5));
                return indicator;
            }
        });
        mMagicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(mMagicIndicator, mViewPager);
        mViewPager.setOffscreenPageLimit(mFragments.size());
        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }

            @Override
            public int getCount() {
                return mFragments.size();
            }
        });
    }

}
